/**
 * @description 路由 channel
 */

const router = require('koa-router')()

const genValidator = require('../middleware/genValidator')
const channelSchema = require('../validator/channels')

// controller
const { createChannels, deleteChannels, updateChannelName, getWorkChannels } = require('../controller/channels/index')

// 路由前缀
router.prefix('/api/channels')

// 创建渠道
router.post('/', genValidator(channelSchema), async ctx => {
  const { username } = ctx.state.user
  const res = await createChannels(username, ctx.request.body)
  ctx.body = res
})

// 删除渠道
router.delete('/:id', async ctx => {
  const { id } = ctx.params // channel ID
  const res = await deleteChannels(id)
  ctx.body = res
})

// 更新渠道名称
router.patch('/updateName/:id', genValidator(channelSchema), async ctx => {
  const { id } = ctx.params
  const { name } = ctx.request.body
  const res = await updateChannelName(id, name)
  ctx.body = res
})

// 根据一个作品的所有渠道
router.get('/getWorkChannels/:workId', async ctx => {
  const { workId } = ctx.params
  const res = await getWorkChannels(workId)
  ctx.body = res
})

module.exports = router
